import {
	getVersion
} from '@/api/common.js';
import i18n from '@/locale/index.js'



export default function AppUpdate() {
	const appleId = '__UNI__31583FC'
	let _this = this
	let platform = uni.getSystemInfoSync().platform

	if (platform != 'android' && platform != 'ios') {
		return;
	}
	//1. 获取当前版本号
	let curversion = '1.1.0';
	plus.runtime.getProperty(plus.runtime.appid, function(widgetInfo) {
		curversion = widgetInfo.version;
	});

	/* 
			2， 用当前版本号提交给服务端，判断是否需要更新，接口返回信息如下
			 versionId		integer(int64)	
			 versionNo	版本号	string	
			 intro	版本描述	string	
			 modify	是否强制更新	boolean	
			 news	是否是最新版本	boolean	
			 url	apk地址	string	
			 createTime	创建时间
	 */
	getVersion().then(rsp => {
		if (rsp.code == 200) {
			var versionInfo = JSON.parse(rsp.data);

			var news = curversion == versionInfo.version;
			var modify = '1' == versionInfo.isUpdate;
			var url = versionInfo.url;
			var createTime = versionInfo.updateTime;
			let currTimeStamp = new Date(createTime).getTime();
			//console.log("是否是最新版= " + news + ",是否是最强制更新=" + modify + ",后台版本号=" + versionInfo.version);
			/* 判断是否是最新版 */
			if (!news) {
				//modify true 强制更新 false 自然更新
				if (modify) {
					uni.showModal({
						title: i18n.t('appUpdate.upgradeNotice'),
						content: i18n.t('appUpdate.upgradeNotice.tip'),
						confirmText: i18n.t('appUpdate.update'),
						showCancel: false,
						success: function(res) {
							if (res.confirm) {
								if (platform == "ios") {
									// plus.runtime.launchApplication({
									// 	action: `itms-apps://itunes.apple.com/cn/app/id${appleId}?mt=8`
									// }, function(e) {
									// 	console.log(
									// 		'Open system default browser failed: ' +
									// 		e.message);
									// });
									//ios 强制退出
									// plus.ios.import("UIApplication").sharedApplication().performSelector("exit");  

								} else {

									uni.showModal({
										title: i18n.t('appUpdate.upgradeNotice'),
										content: i18n.t('appUpdate.upgradeNotice.downlaod'),
										confirmText: i18n.t('common.submit'),
										showCancel: false,
										success: function(res) {
											if (res.confirm) {
												// 安卓退出app     
												plus.runtime.quit();
											}
										}
									});

									// uni.showToast({
									// 	icon: 'none',
									// 	mask: true,
									// 	title: i18n.t('appUpdate.newVersionInfo'),
									// 	duration: 5000
									// });
									//设置 最新版本apk的下载链接 这是固定的
									let downloadApkUrl = url
									console.log(downloadApkUrl);
									plus.runtime.openURL(downloadApkUrl);
								}

							}
						}
					})
				} else {
					uni.getStorage({
						key: 'tip_version_update_time',
						success: function(res) {
							var lastTimeStamp = res.data; // 本地时间戳
							var tipTimeLength = 0; // 时间间隔
							let cha = lastTimeStamp + tipTimeLength - currTimeStamp;
							if (lastTimeStamp + tipTimeLength > currTimeStamp) {
								//console.log('当后台时间戳大于本地时间戳才会进入');
							} else {
								//console.log('立即更新');
								//重新设置时间戳
								uni.setStorage({
									key: 'tip_version_update_time',
									data: currTimeStamp,
									success: function() {
										console.log('setStorage-success');
									}
								});


								uni.showModal({
									title: i18n.t('appUpdate.normalVersion'),
									content: i18n.t('appUpdate.normalVersion.tip'),
									confirmText: i18n.t('appUpdate.update'),
									cancelText: i18n.t('common.cancel'),
									success: function(res) {
										if (res.confirm) {
											if (platform == "ios") {
												plus.runtime.launchApplication({
													action: `itms-apps://itunes.apple.com/cn/app/id${appleId}?mt=8`
												}, function(e) {
													console.log(
														'Open system default browser failed: ' +
														e.message);
												});

											} else {
												uni.showToast({
													icon: 'none',
													mask: true,
													title: i18n.t(
														'appUpdate.newVersionInfo'
													),
													duration: 5000
												});
												//设置 最新版本apk的下载链接 这是固定的 每次把包放在这个链接里里面即可 由后端制作
												let downloadApkUrl = url
												plus.runtime.openURL(
													downloadApkUrl);
											}

										} else if (res.cancel) {
											//console.log('下次一定');
										}
									}
								});


							}
						},
						fail: function(res) {
							uni.setStorage({
								key: 'tip_version_update_time',
								data: currTimeStamp,
								success: function() {
									console.log('setStorage-success');
								}
							});
						}
					});

					console.log(createTime, 'createTimecreateTime')
				}
			}

		}
	});


	// uni.request({
	// 	url: BASE_URL + '/choker-api-test/version/get',
	// 	method: 'GET',
	// 	data: {
	// 		versionNo: curversion
	// 	},
	// 	success: res => {
	// 		const {
	// 			versionNo,
	// 			intro,
	// 			modify,
	// 			news,
	// 			url,
	// 			createTime
	// 		} = res.data.data
	// 		let currTimeStamp = new Date(createTime).getTime();
	// 		console.log("是否是最新版= " + news + ",是否是最强制更新=" + modify + ",后台版本号=" + versionNo);

	// 	},
	// 	fail: () => {},
	// 	complete: () => {}
	// })




}